﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace BookMS
{
    public partial class Login : Form
    {
        // 从配置文件获取数据库连接字符串
        private readonly string _connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        public Login()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (UserBox.Text != "" && PasswordBox.Text != "")
            {
                login();
            }

            else
            {
                MessageBox.Show("输入项不能为空，请重新输入");
            }


        }


        //public Boolean login()//登录方法，验证是否允许登录，允许返回真
        public void login()//登录方法，验证是否允许登录，允许返回真
        {
            //用户
            if (radioButtonUser.Checked == true)
            {
                Dao dao = new Dao();//使用对数据库操作的对象
             
                //SELECT *  FROM t_user where name ='1'and password = '22'
                //SELECT *  FROM t_user where name ='"++"'and password = '22'
                //string sql1 = "select * from t_user where name='" + UserBox.Text + "'and password= '" + PasswordBox.Text + "'";
                //string sql2 = String.Format("select * from t_user where name='{0}' and password='{1}'", UserBox.Text, PasswordBox.Text);
                string loginUserSql = $"select * from t_user where name='{UserBox.Text}'and password='{PasswordBox.Text}'";
                //MessageBox.Show(sql1+'\n'+sql2+'\n'+sql3);
                IDataReader dc =dao.read(loginUserSql);//读取结果解
                //dc.Read();
                //MessageBox.Show(dc[0].ToString() ,dc["name"].ToString());
                //MessageBox.Show(dc[0].ToString() + dc["name"].ToString());

                if (dc.Read()) {
                    Data.UID = dc["uid"].ToString();//登录之后操作业务，进行借书和还书就方便一些
                    Data.UName = dc["name"].ToString();
                    MessageBox.Show(dc["name"].ToString() + "普通用户登录成功");
                    UserPage u=new UserPage();
                    this.Hide();
                    u.ShowDialog();
                    this.Show();
                
                }
                else
                {
                    MessageBox.Show("普通用户登录失败");
                }
                dao.Daoclose();//查询完关闭数据库连接


            }
            //管理员
            if (radioButtonAdmin.Checked == true)
            {
                Dao dao = new Dao();//使用对数据库操作的对象
                string loginAdminSql = $"select * from t_admin where name='{UserBox.Text}'and password='{PasswordBox.Text}'";
                IDataReader dc = dao.read(loginAdminSql);//读取结果解
                if (dc.Read())
                {
                    //MessageBox.Show("管理员登录成功");
                    AdminPage a = new AdminPage();
                    //this.Hide();
                    a.ShowDialog();//点击登录只能打开一个页面
                    //a.Show();//点击登录会打开多个页面。
                    //this.Show();

                }
                else
                {
                    //MessageBox.Show("管理员登录失败");
                }
                dao.Daoclose();

            }
            //return true;

        }

        private void UserBox_TextChanged(object sender, EventArgs e)
        {

        }

        //public void login()//登录方法，验证是否允许登录，允许返回真
        //{
        //    //用户
        //    if (radioButtonUser.Checked == true)
        //    {
        //        Dao dao = new Dao();//使用对数据库操作的对象
        //        string sql = $"  SELECT *  FROM t_user where name ='{UserBox.Text}'and password = '{PasswordBox.Text}'";
        //        IDataAdapter dc = (IDataAdapter)dao.read(sql);
        //        if (dc.Read())
        //        {
        //            MessageBox.Show("登录成功");
        //        }
        //        else
        //        {
        //            MessageBox.Show("登录失败");
        //        }
        //        dao.Daoclose();
        //    }


        //    //管理员
        //    if (radioButtonAdmin.Checked == true)
        //    {
        //        Dao dao = new Dao();
        //        string sql = $"  SELECT *  FROM t_admin where name ='{UserBox.Text}'and password = '{PasswordBox.Text}'";
        //        IDataAdapter dc = (IDataAdapter)dao.read(sql);
        //        if (dc.Read())
        //        {
        //            MessageBox.Show("登录成功");
        //        }
        //        else
        //        {
        //            MessageBox.Show("登录失败");
        //        }
        //        dao.Daoclose();
        //    }
        //    MessageBox.Show("单选框请先选中");

        //}




    }
}

